﻿/*******************************************************************************
#                    QUERecordDetailBroker.cs
#  Comment:
#
#  Current Version: V1.0
#  Author: 
#
#  History List:
#  V1.0    Created by Onions@2009-9-17 10:22:15
#
#******************************************************************************/
#region Usings
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using SYS.Basic;

using ExpertMIS.Brokers;
using ExpertMIS.Entities;
using System.Collections;
#endregion

namespace ExpertMIS.Brokers
{
    public class QUERecordDetailBroker : ExpertMISBaseBroker
    {
        #region Private Methods

        private object UpdateRecordDetailEntity(IEntity entity)
        {
            return SqlMap.Update("UpdateRecordDetailEntity", entity);
        }

        private object InsertRecordDetailEntity(IEntity entity)
        {
            return SqlMap.Insert("InsertRecordDetailEntity", entity);
        }

        private object DeleteRecordDetailEntity(IEntity entity)
        {
            return SqlMap.Delete("DeleteRecordDetailEntity", entity);
        }

        #endregion

        #region Override Methods

        protected override IEntity ProcessGetEntity(System.Collections.Hashtable keys)
        {
            if (!keys.Contains("OID")) throw new Exception("invalid request parameters.");
            return SqlMap.QueryForObject<IEntity>("SelectRecordDetailEntityByOID", keys["OID"] as string);
        }

        protected override bool ProcessSaveEntity(IEntity entity, Hashtable request)
        {
            if (entity.EntityState == EntityState.New)
            {
                string newID = InsertRecordDetailEntity(entity).ToString();
                RecordDetailEntity record = entity as RecordDetailEntity;
                record.OID = newID;

                decimal avgGrade = (decimal)SqlMap.QueryForObject("SelectAvageGradeByExpertID", record.ExpertID);
                ExpertBroker broker = new ExpertBroker();
                broker.UpdateExpertTotalGrade(record.ExpertID, avgGrade);

                entity.EntityState = EntityState.Old;
            }
            else
            {
                UpdateRecordDetailEntity(entity);
            }

            return true;
        }

        protected override IEntity ProcessNewEntity()
        {
            RecordDetailEntity entity = new RecordDetailEntity();

            // initial entity object

            return entity;
        }

        protected override bool ProcessRemoveEntity(IEntity entity, Hashtable request)
        {
            try
            {
                DeleteRecordDetailEntity(entity);
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion

        #region Public Methods

        #endregion
    }
}